<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:easyui="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1,maximum-scale=1,user-scalable=no,viewport-fit=cover">
    <title>首页</title>
    <script type="text/javascript" src="../../static/easyui/boot.js" th:src="@{/static/easyui/boot.js}"></script>
</head>
<body class="easyui-layout">
<!-- begin of header -->
<div class="lg-header" data-options="region:'north',border:false,split:true">
    <div class="lg-header-left">
        <h1>EasyUI</h1>
    </div>
    <div class="lg-header-right">
        <p th:text="${name}"></p>
        <p>
            <a href="javascript:void(0)">修改密码</a>
            |<a href="javascript:void(0)">安全退出</a>
        </p>
    </div>
</div>
<!-- end of header -->

<!-- begin of sidebar -->
<div class="lg-sidebar" data-options="region:'west',split:true,border:true,title:'导航菜单'">
    <!-- <div id="sm" class="easyui-sidemenu" data-options="data:data,multiple:false,onSelect:onSelect"></div> -->
    <div id="sm"></div>
</div>
<!-- end of sidebar -->

<!-- begin of main -->
<div class="lg-main" data-options="region:'center'">
    <div id="main-tabs" class="easyui-tabs" data-options="border:false,fit:true,tools:'#tab-tools'">
<!--        <div title="首页"  data-options="href:'/sysMenu/index.do',closable:false,iconCls:'fa fa-home',cls:'pd3'"></div>-->
    </div>
</div>
<div id="tab-tools">
    <easyui:button label="" iconCls="fa fa-home" plain="true" name="butAdd" onclick="gotoHomeTab()"/>
    <easyui:button label="" iconCls="fa fa-refresh" plain="true" name="butAdd" onclick="refreshTab()"/>
    <easyui:button label="" iconCls="fa fa-power-off" plain="true" name="butAdd" onclick="clearTab()"/>
</div>
<!-- end of main -->

<!-- begin of footer -->
<div class="lg-footer" style="font-size: 10px" data-options="region:'south',border:true,split:true">
    &copy; 2024 F12 All Rights Reserved
</div>
<!-- end of footer -->

</body>
</html>
<script type="text/javascript">
    $(document).ready(function () {
        // 定时检查AccessToken的过期时间
        //setInterval(refToken, 60 * 1000);
        ajaxRequest({
            url:"/main/getMenu.do",
            type:"get"
        },function(e){
            $("#sm").sidemenu({
                data:eval('(' + e.result + ')'),
                onSelect: function(item){
                    addTab(item.text,item.url, item.iconCls);
                },
                animate:true,
                multiple:false,
                border:false,
                width:150
            });
        });
    });

    /**
     * Name 添加菜单选项
     * Param title 名称
     * Param href 链接
     * Param iconCls 图标样式
     */
    function addTab(title, href, iconCls){
        let _id=guid();
        var tabPanel = $('#main-tabs');
        if(!tabPanel.tabs('exists',title)) {
            let urlStr = new URL(location.origin + apiPrefix(href));
            urlStr.searchParams.append('iframe', _id);
            var content = '<iframe scrolling="auto" frameborder="0"  src="' + urlStr.toString() + '" style="width:100%;height:99%;"></iframe>';
            tabPanel.tabs('add', {
                title: title,
                content: content,
                iconCls: iconCls,
                fit: true,
                cls: 'pd3',
                closable: true
            });
        }
        else {
            tabPanel.tabs('select',title);
        }
        document.getElementsByTagName("title")[0].innerText = title;
    }
    //回到首页
    function gotoHomeTab() {
        var tabPanel = $('#main-tabs');
        if(tabPanel.tabs('exists',0)){
            tabPanel.tabs('select',0);
        }
    }
    //刷新当前选中的Tab
    function refreshTab(){
        var tab = $('#main-tabs').tabs('getSelected');  // 获取选择的面板
        var index = $('#main-tabs').tabs('getTabIndex',tab);
        if(Number(index)!=0) {
            $('#main-tabs').tabs('update', {
                tab: tab,
                options: {
                    content: tab[0].innerHTML
                }
            });
        }else{
            //首页
            var url = $(tab.panel('options')).attr('href');
            tab.panel('refresh', url);
        }
    }
    //关闭Tab
    function clearTab(){
        var tabPanel = $('#main-tabs');
        tabPanel.tabs('close',1);
    }


    function refToken() {
        // 计算AccessToken的剩余有效时间
        const expireTime=localStorage.getItem("expireTime");
        if(expireTime) {
            const remainTime = expireTime - Date.now();
            // 如果AccessToken的剩余有效时间小于5分钟，则发送续期请求
            if (remainTime < 5 * 60 * 1000) {
                clearInterval(refToken);//停止
                ajaxRequest({url: "/sysAccount/refToken.do", type: "post"}, function (res) {
                    if(res.code==0) {
                        localStorage.removeItem("expireTime");
                        localStorage.removeItem("Authorization");
                        localStorage.setItem("Authorization", res.result.token);
                        localStorage.setItem("expireTime", res.result.expireTime);
                        setInterval(refToken, 60 * 1000);
                    }
                });
            }
        }
    }
</script>
</html>
